package com.hzzc.util;

import com.hzzc.BizException;
import org.apache.commons.lang3.StringUtils;
import org.mortbay.util.MultiMap;
import org.mortbay.util.UrlEncoded;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class UrlUtil {

    public static String cleanParameter(String url, List<String> names) {
        try {
            URL urlData = new URL(url);
            MultiMap multiMap = new MultiMap();
            UrlEncoded.decodeTo(StringUtils.isEmpty(urlData.getQuery()) ? "" : urlData.getQuery(), multiMap, "UTF-8");
            for (String name : names) {
                multiMap.remove(name);
            }
            multiMap.remove("");
            return urlData.getProtocol() + "://" + urlData.getHost() + ":" + (urlData.getPort() == -1 ? "" : urlData.getPort()) + urlData.getPath() + (multiMap.size() > 0 ? ("?" + UrlEncoded.encode(multiMap, "utf-8", false)) : "");
        } catch (MalformedURLException e) {
            throw new BizException("地址转换异常！", e);
        }
    }

}
